import { app, BrowserWindow } from 'electron'

if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
  const childProcess=require('child_process');
  childProcess.exec('cd resources\\server && start run_server.vbs');
}else{
  //运行后台服务
  const childProcess=require('child_process');
  childProcess.exec('cd server && start run_server.vbs');
}
let mainWindow
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

function createWindow () {
  /**
   * Initial window options
   */
  const electron = require('electron')
  electron.Menu.setApplicationMenu(null); //不显示目录
  mainWindow = new BrowserWindow({
    height: 650,
    useContentSize: true,
    width: 950,
    webPreferences: {
      nodeIntegration: true,
    }
  })

  mainWindow.loadURL(winURL)
  
  mainWindow.on('closed', () => {
    mainWindow = null
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    if (process.env.NODE_ENV !== 'development') {
      const childProcess=require('child_process');
      childProcess.exec('cd resources\\server && start stop_server.vbs');
    }else{
      //运行后台服务
      const childProcess=require('child_process');
      childProcess.exec('cd server && start stop_server.vbs');
    }
    app.quit();
  }
});

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})
